package com.kellycheung.chicken_go_go;

import java.util.HashMap;

import android.app.Activity;
import android.content.SharedPreferences;

public class GPerferences {
	protected GameEngine geMain;
	protected HashMap<String,Integer> hmPerf;
	protected SharedPreferences sp;
	protected SharedPreferences.Editor spe;

	public GPerferences( GameEngine p_ge ) {
		try {
			this.geMain = p_ge;
			this.hmPerf = new HashMap<String,Integer>();
			this.sp = this.geMain.aApp.getPreferences(Activity.MODE_PRIVATE);
			this.spe = this.sp.edit();
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "GPerferences" );
			GameUtil.ErrorHandler( this , p_e );
		}
	}
	
	public void destroy() {
		try {
			this.spe = null;
			this.sp = null;
			this.geMain = null;
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "GPerferences.destroy" );
			GameUtil.ErrorHandler( this , p_e );
		}
	}
    
    public void setPerference( String p_sKey , int p_iVal ) {
    	try {
	    	this.spe.putInt( p_sKey , p_iVal );
	    	this.spe.commit();
	    	this.hmPerf.put( p_sKey , p_iVal );
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "GPerferences.setPerference" );
			GameUtil.ErrorHandler( this , p_e );
		}
    }
    
    public int getPerference( String p_sKey , int p_iDefVal ) {
    	try {
    		if ( !this.hmPerf.containsKey( p_sKey ) ) {
	    		if ( this.sp.getInt( p_sKey , 0 ) == 0 ) {
	    			this.setPerference( p_sKey , p_iDefVal );
	    		}
    		}
    		
			this.hmPerf.put( p_sKey , this.sp.getInt( p_sKey , p_iDefVal ) );
			
	    	return this.hmPerf.get( p_sKey );
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "GPerferences.getPerference" );
			GameUtil.ErrorHandler( this , p_e );
		}
    	
    	return p_iDefVal;
    }
    
    public void removePerference() {
    	try {
	    	this.spe.clear();
	    	this.spe.commit();
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "GPerferences.removePerference" );
			GameUtil.ErrorHandler( this , p_e );
		}
    }
}
